package com.ruoyi.verificationDetail.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.verificationDetail.domain.VerificationDetail;
import com.ruoyi.verificationDetail.service.IVerificationDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.verificationDetail.domain.VerificationQRCodeRequest;
import com.ruoyi.verificationDetail.domain.VerificationQRCodeResult;

/**
 * 核销码明细信息Controller
 * 
 * @author ruoyi
 * @date 2025-11-18
 */
@RestController
@RequestMapping("/ruoyi/verificationDetail")
public class VerificationDetailController extends BaseController
{
    @Autowired
    private IVerificationDetailService verificationDetailService;

    /**
     * 查询核销码明细信息列表
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:verificationDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(VerificationDetail verificationDetail)
    {
        startPage();
        List<VerificationDetail> list = verificationDetailService.selectVerificationDetailList(verificationDetail);
        return getDataTable(list);
    }

    /**
     * 导出核销码明细信息列表
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:verificationDetail:export')")
    @Log(title = "核销码明细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VerificationDetail verificationDetail)
    {
        List<VerificationDetail> list = verificationDetailService.selectVerificationDetailList(verificationDetail);
        ExcelUtil<VerificationDetail> util = new ExcelUtil<VerificationDetail>(VerificationDetail.class);
        util.exportExcel(response, list, "核销码明细信息数据");
    }

    /**
     * 获取核销码明细信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:verificationDetail:query')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId)
    {
        return success(verificationDetailService.selectVerificationDetailByDetailId(detailId));
    }

    /**
     * 新增核销码明细信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:verificationDetail:add')")
    @Log(title = "核销码明细信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VerificationDetail verificationDetail)
    {
        return toAjax(verificationDetailService.insertVerificationDetail(verificationDetail));
    }

    /**
     * 修改核销码明细信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:verificationDetail:edit')")
    @Log(title = "核销码明细信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VerificationDetail verificationDetail)
    {
        return toAjax(verificationDetailService.updateVerificationDetail(verificationDetail));
    }

    /**
     * 删除核销码明细信息
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi:verificationDetail:remove')")
    @Log(title = "核销码明细信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable Long[] detailIds)
    {
        return toAjax(verificationDetailService.deleteVerificationDetailByDetailIds(detailIds));
    }

    /**
     * 生成核销二维码
     * @param request 核销二维码请求参数
     * @return 二维码生成结果
     */
    @PostMapping("/generateVerificationQRCode")
    public AjaxResult generateVerificationQRCode(@RequestBody VerificationQRCodeRequest request) {
        try {
            // 验证请求参数
            if (request.getVerificationCode() == null) {
                return AjaxResult.error("核销码不能为空");
            }

            if (request.getOrderId() == null) {
                return AjaxResult.error("订单ID不能为空");
            }

            // 验证并设置默认尺寸
            if (request.getWidth() != null && (request.getWidth() < 100 || request.getWidth() > 1000)) {
                return AjaxResult.error("二维码宽度必须在100-1000像素之间");
            }

            if (request.getHeight() != null && (request.getHeight() < 100 || request.getHeight() > 1000)) {
                return AjaxResult.error("二维码高度必须在100-1000像素之间");
            }

            // 验证versionType参数
            if (request.getVersionType() != null &&
                    !("test".equals(request.getVersionType()) ||
                            "development".equals(request.getVersionType()) ||
                            "production".equals(request.getVersionType()))) {
                return AjaxResult.error("版本类型只能是: production(线上版), test(体验版), development(开发版)");
            }

            VerificationQRCodeResult result = verificationDetailService.generateVerificationQRCode(request);
            return AjaxResult.success("核销二维码生成成功", result);
        } catch (Exception e) {
            logger.error("生成核销二维码失败", e);
            return AjaxResult.error("核销二维码生成失败：" + e.getMessage());
        }
    }
}
